<template>
  <div class="app-container">
    <div class="app-container">
      <el-row :gutter="20">
        <!--部门数据-->
        <el-col :span="4" :xs="24">
          <div class="head-container">
            <el-input
              v-model="domainName"
              placeholder="请输入领域名称"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
            />
          </div>
          <div class="head-container" style="min-width:400px">
            <el-tree
              :data="domainOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"

              ref="tree"
              @node-click="handleNodeClick"
            />
          </div>
        </el-col>
        <!--用户数据-->
        <el-col :span="20" :xs="24" >


    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="问题题目" prop="orderNum">
        <el-input
          v-model="queryParams.label"
          placeholder="问题题目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="问题等级" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择问题等级" clearable>
          <el-option
            v-for="dict in dict.type.sys_question_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="问题状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择问题状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:survey:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:survey:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:survey:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:survey:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="surveyList" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="问题id" align="center" prop="questionId" />-->
      <el-table-column label="问题题目" align="center" prop="label" show-overflow-tooltip />
      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="问题分值" align="center" prop="score" />
      <el-table-column label="问题类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.question_show_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="问题选项" align="center" prop="options" />-->
      <el-table-column label="问题等级" align="center" prop="level">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_question_level" :value="scope.row.level"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="领域Id" align="center" prop="domain" >-->
<!--&lt;!&ndash;        <template slot-scope="scope">&ndash;&gt;-->
<!--&lt;!&ndash;          <dict-tag :options="dict.type.sys_question_level" :value="scope.row.domain"/>&ndash;&gt;-->
<!--&lt;!&ndash;        </template>&ndash;&gt;-->
<!--      </el-table-column>-->
<!--      <el-table-column label="问题描述" align="center" prop="questionDesc" />-->
<!--      <el-table-column label="问题建议" align="center" prop="advice" />-->
<!--      <el-table-column label="默认值" align="center" prop="defaultValue" />-->
<!--      <el-table-column label="验证规则" align="center" prop="reglist" />-->
      <el-table-column label="问题状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="是否只读" align="center" prop="readonly">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.readonly"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="是否可用" align="center" prop="disabled">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.disabled"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="是否必须" align="center" prop="required">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.required"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:survey:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:survey:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

          <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">

              <el-form-item label="问题题目" prop="label">
                <el-input v-model="form.label" type="textarea" placeholder="请输入内容" />
              </el-form-item>
              <el-form-item label="问题类型" prop="type">
                <el-select v-model="form.type" placeholder="请选择问题类型">
                  <el-option
                    v-for="dict in dict.type.question_show_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="问题选项" prop="options">
                <el-input v-model="form.options" type="textarea" placeholder="请输入内容" />
              </el-form-item>
              <el-form-item label="问题等级" prop="level">
                <el-select v-model="form.level" placeholder="请选择问题等级" clearable>
                  <el-option
                    v-for="dict in dict.type.sys_question_level"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="所属领域" prop="domain">
                <el-select v-model="form.domain" placeholder="请选择问题所属领域" clearable>
                  <el-option
                    v-for="dict in dict.type.sys_question_level"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="问题描述" prop="questionDesc">
                <el-input v-model="form.questionDesc" type="textarea" placeholder="请输入内容" />
              </el-form-item>
              <el-form-item label="显示顺序" prop="orderNum">
                <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
              </el-form-item>
              <el-form-item label="问题分值" prop="score">
                <el-input v-model="form.score" placeholder="请输入问题分值" />
              </el-form-item>
              <el-form-item label="问题建议" prop="advice">
                <el-input v-model="form.advice" type="textarea" placeholder="请输入内容" />
              </el-form-item>
              <el-form-item label="默认值" prop="defaultValue">
                <el-input v-model="form.defaultValue" placeholder="请输入默认值" />
              </el-form-item>
              <el-form-item label="验证规则" prop="reglist">
                <el-input v-model="form.reglist" type="textarea" placeholder="请输入内容" />
              </el-form-item>
              <el-form-item label="问题状态">
                <el-radio-group v-model="form.status">
                  <el-radio
                    v-for="dict in dict.type.sys_normal_disable"
                    :key="dict.value"
                    :label="dict.value"
                  >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="是否只读">
                <el-radio-group v-model="form.readonly">
                  <el-radio
                    v-for="dict in dict.type.sys_yes_no"
                    :key="dict.value"
                    :label="dict.value"
                  >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="是否可用">
                <el-radio-group v-model="form.disabled">
                  <el-radio
                    v-for="dict in dict.type.sys_yes_no"
                    :key="dict.value"
                    :label="dict.value"
                  >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="是否必须">
                <el-radio-group v-model="form.required">
                  <el-radio
                    v-for="dict in dict.type.sys_yes_no"
                    :key="dict.value"
                    :label="dict.value"
                  >{{dict.label}}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
            </div>
          </el-dialog>
        </el-col>
      </el-row>

    </div>
  </div>
</template>

<script>
import { listSurvey, getSurvey, delSurvey, addSurvey, updateSurvey } from "@/api/system/generalsurvey";
import {treeselect, treeselectbyId} from "@/api/system/questiondomain";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";



export default {
  name: "Survey",
  dicts: ['question_show_type', 'sys_normal_disable','sys_question_level','sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 普查问卷问题表格数据
      surveyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      domainOptions: undefined,
      domainName: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNum: null,
        score: null,
        label: null,
        type: null,
        options: null,
        level: null,
        domain: null,
        questionDesc: null,
        advice: null,
        defaultValue: null,
        reglist: null,
        status: null,
        readonly: null,
        disabled: null,
        required: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        label: [
          { required: true, message: "问题题目不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "问题类型不能为空", trigger: "change" }
        ],
        level: [
          { required: true, message: "问题等级不能为空", trigger: "blur" }
        ],
        domain: [
          { required: true, message: "领域Id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getConfigKey("sys.user.initPassword").then(response => {
      this.initPassword = response.msg;
    });
  },
  methods: {
    /** 查询普查问卷问题列表 */
    getList() {
      this.loading = true;
      listSurvey(this.queryParams).then(response => {
        this.surveyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTreeselect() {
      treeselect().then(response => {
        this.domainOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.domain = data.id;
      this.handleQuery();
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.userName + '"用户吗？').then(function() {
        return changeUserStatus(row.userId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        questionId: null,
        orderNum: null,
        score: null,
        label: null,
        type: null,
        options: null,
        level: null,
        domain: null,
        questionDesc: null,
        advice: null,
        defaultValue: null,
        reglist: null,
        status: "0",
        readonly: null,
        disabled: null,
        required: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.questionId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加普查问卷问题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const questionId = row.questionId || this.ids
      getSurvey(questionId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改普查问卷问题";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.questionId != null) {
            updateSurvey(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSurvey(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const questionIds = row.questionId || this.ids;
      this.$modal.confirm('是否确认删除普查问卷问题编号为"' + questionIds + '"的数据项？').then(function() {
        return delSurvey(questionIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/survey/export', {
        ...this.queryParams
      }, `survey_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
